<?php
YII::import('application.components.bid.class.BidComponent');

class BidExpire extends BidComponent
{

    public function run($expireTime)
    {
        $q = 0;
        $amount = $this->getPrototype()->getAmount();
        switch ($this->getPrototype()->action) {
            case BidModel::ACTION_COST:
                do {
                    $costAmount = abs($amount);
                    $getRowCountSQL = '
                        SELECT
                            sum(customer_bid_expire.bid_num) AS last_bid_sum
                        FROM
                            customer_bid_expire
                        WHERE
                            customer_bid_expire.customer_id = ' . $this->getPrototype()->customer_id . '
                    ';
                    $getRowCount = Yii::app()->db->createCommand($getRowCountSQL)->queryRow();
                    if ($getRowCount['last_bid_sum'] < $costAmount) {
                        throw new CException(Yii::t('yii', 'Expire Bid overflow'));
                    }
                    
                    
                    
                    $bidExpireSQL = '
                        UPDATE
                            customer_bid_expire
                        SET
                            customer_bid_expire.bid_num = @bid_num := customer_bid_expire.bid_num - ' . $costAmount . ',
                            customer_bid_expire.id = @bid_row_id := customer_bid_expire.id
                        WHERE
                            customer_bid_expire.customer_id = ' . $this->getPrototype()->customer_id . '
                        LIMIT 1
                    ';
                    $expire = Yii::app()->db->createCommand($bidExpireSQL)->execute();
                    $getAfterExpireSQL = '
                    SELECT
                        ifnull(@bid_num,0) AS bid_num,
                        ifnull(@bid_row_id,0) AS row_id
                    ';
                    $bidNum = Yii::app()->db->createCommand($getAfterExpireSQL)->queryRow();
                    $amount = $bidNum['bid_num'];
                    
                    if ($amount <= 0 && ! empty($bidNum['row_id'])) {
                        $clearRowSQL = '
                            DELETE FROM customer_bid_expire WHERE customer_bid_expire.id = ' . $bidNum['row_id'] . '
                        ';
                        $delRow = Yii::app()->db->createCommand($clearRowSQL)->execute();
                    }
                } while ($amount < 0);
                break;
            case BidModel::ACTION_EARN:
                
                $bidExpireSQL = '
                    INSERT INTO
                        customer_bid_expire
                    VALUES (
                        default,
                        "' . $this->getPrototype()->customer_id . '",
                        "' . $this->getPrototype()->getLastHistoryId() . '",
                        "' . $this->getPrototype()->bid_type . '",
                        "' . $amount . '",
                        "' . $amount . '",
                        "' . time() . '",
                        "' . (time() + $expireTime) . '"
                    )
                ';
                return Yii::app()->db->createCommand($bidExpireSQL)->execute();
                break;
        }
    }
}